package parser;

import java.util.ArrayList;
import java.util.List;

public abstract class MySimpleNode extends SimpleNode 
{

	public MySimpleNode(int id)
	{
		super(id);
	}

	public MySimpleNode(XQueryParser p, int id)
	{
		super(p, id);
	}

	public MySimpleNode jjtGetParent()
	{

		return (MySimpleNode) super.jjtGetParent();
	}

	public MySimpleNode jjtGetChild(int i)
	{
		return (MySimpleNode) super.jjtGetChild(i);
	}

	public List<? extends MySimpleNode> jjtGetChildren()
	{
		List<MySimpleNode> retval = new ArrayList<MySimpleNode>();
		for (int i = 0; i < this.jjtGetNumChildren(); i++)
			retval.add(this.jjtGetChild(i));
		return retval;
	}

	public void jjtPrependFirstChild(Node n)
	{
		if (children == null)
		{
			children = new Node[1];
		} else
		{
			Node c[] = new Node[children.length + 1];
			System.arraycopy(children, 0, c, 1, children.length);
			children = c;
		}
		children[0] = n;
	}

	public void jjtSetSecondChild(Node n)
	{
		if (children == null)
		{
			children = new Node[2];
		} else
		{
			Node c[] = new Node[children.length + 1];
			System.arraycopy(children, 0, c, 1, children.length);
			children = c;
		}
		children[1] = n;
	}

	public static void errorExit(String message)
	{
		{
			System.err.println(message);
			System.exit(-1);
		}
	}

	public static void fixParent(JJTXQueryParserState jjtree)
	{
		MySimpleNode parent = (MySimpleNode) jjtree.popNode();
		MySimpleNode child = (MySimpleNode) jjtree.popNode();
		parent.jjtPrependFirstChild(child);
		child.jjtSetParent(parent);
		jjtree.pushNode(parent);
	}

	public static void fixParentAP(JJTXQueryParserState jjtree)
	{
		MySimpleNode child = (MySimpleNode) jjtree.popNode();
		MySimpleNode parent = (MySimpleNode) jjtree.popNode();
		parent.jjtPrependFirstChild(child);
		child.jjtSetParent(parent);
		jjtree.pushNode(parent);
	}

	public static void fixParent2(JJTXQueryParserState jjtree)
	{
		fixParent(jjtree);
		fixParent(jjtree);
	}

	public static void jjtSetChild(MySimpleNode parent, MySimpleNode child,
			int childNum)
	{
		parent.jjtAddChild(child, childNum);
		child.jjtSetParent(parent);
	}
	
	public static int jjtGetChildNum(MySimpleNode parent, MySimpleNode child)
	{
		for(int i = 0; i<parent.jjtGetNumChildren(); i++)
		{
			if(parent.jjtGetChild(i) == child)
				return i;
		}
		return -1;
	}

	public static void jjtAppendChild(MySimpleNode parent, MySimpleNode child)
	{
		jjtSetChild(parent, child, parent.jjtGetNumChildren());
	}
}